import json

from room_device_online_status import room_online, device_online, room_device
from mqtt import client
import time
import threading

room_topic = '/room/online'
device_topic = '/device/online'

def send_room_online():
    for room in room_online:
        # send_msg('/room/online', room)
        # print(room)
        client.publish(room_topic, json.dumps(room))


def send_device_online():
    for device in device_online:
        # send_msg('/room/online', room)
        client.publish(device_topic, json.dumps(device))


def center_send_msg():
    for room in room_device:
        client.publish(room_topic, json.dumps(room['center']))
        if room['center']['status'] == 1:
            for device in room['other']:
                client.publish(device_topic, json.dumps(device))


def start_centerControl():
    while True:
        # # 发送房间在线
        # threading.Thread(target=send_room_online, args=[]).start()
        # # send_room_online()
        # # 发送设备在线
        # threading.Thread(target=send_device_online, args=[]).start()
        # # send_device_online()
        # time.sleep(5)

        threading.Thread(target=center_send_msg, args=[]).start()
        time.sleep(10)


if __name__ == "__main__":
    start_centerControl()
